var co = require('co');
var seqModule = require('../../models/index');
var db = new seqModule();
var util = require('../../common/util');

var meetSpeakController = {
    getSpeakByMeetId: function (req, res) {
        co(function () {
            var page = parseInt(req.query.page);
            var params = {};
            var paramT = {};
            var include = [{model:db.speakHostModule}]
            if (req.params.meetId != undefined) {
                params["meetId"] = req.params.meetId;
                paramT["where"] = params;
                paramT["order"] =  [[ db.speakHostModule, 'name','ASC' ] ];
                if (req.query.page != undefined && req.query.rows != undefined) {
                    paramT["offset"] = parseInt(req.query.page);
                    paramT["limit"] = parseInt(req.query.rows);
                }
                db.meetSpeakModule.findAndCountAll(paramT).then(function (result) {
                    result.page = page;
                    res.status(200).json(result);
                });
            }else{
                res.status(402).json({"message":"参数错误！"});
            }
           
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    },
    addMeetSpeak: function (req, res) {
        co(function () {
            req.body.id = util.getUUID();
            req.body.createTime = util.getNowTime();
            db.meetSpeakModule.create(req.body).then(function (result) {
                res.status(200).json(result);
            });
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    },
    addMoreMeetSpeak: function (req, res) {
        co(function () {
           var meetId = req.body.meetId;
           var speakIds = req.body.speakIds;
           var speakArr = speakIds.split(",");
           var sql = "insert into meet_speak values ";
           for(var i=0;i<speakArr.length;i++){
               sql +="('"+util.getUUID()+"','"+meetId+"','"+speakArr[i]+"','"+util.getNowTime()+"'),";
           }
           sql = sql.substring(0,sql.lastIndexOf(","));
            db.sequelize.query(sql).spread(function (result) {
                res.status(200).json(result);
            });
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    },
    delMeetById:function(req,res){
        co(function () {
           var paramT = {};
           if(req.params.id != undefined){
               paramT.where = req.params;
            db.meetSpeakModule.destroy(paramT).then(function (results) {
                res.status(200).json(results);
            });
           }else{
            res.status(402).json({"message":"参数错误！"});
           }
           
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    },
    delMeetByIds: function (req, res) {
        co(function () {
            var paramT = {};
            var paramU = {};
            var StringId = req.query.params;//传过来的params是以‘,’隔开的字符串
            var arr = StringId.split(",");
            paramU['id'] = [arr];
            paramT['where'] = paramU;
            //destroy、方法就是删除记录的方法 paramT是删除条件
            db.meetSpeakModule.destroy(paramT).then(function (results) {
                res.status(200).json(results);
            });
        }).catch(function (e) {
            res.status(500).json({"message":"服务异常！"});
            console.log(e);
        })
    }
}
module.exports = meetSpeakController;